//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;

namespace Microsoft.TridentWordAddIn.Presentation
{
    internal partial class CloudDatabaseStoreInformation : UserControl
    {
        #region Constructors
        
        /// <summary>
        /// Initializes a new instance of the <see cref="CloudDatabaseStoreInformation"/> class.
        /// </summary>
        public CloudDatabaseStoreInformation()
        {
            InitializeComponent();
        }

        #endregion 

        /// <summary>
        /// Occurs when [on validation].
        /// </summary>
        public event EventHandler<ConnectionValidateEventArgs> OnValidation;

        #region Properties

        /// <summary>
        /// Gets or sets the authority id.
        /// </summary>
        /// <value>The authority id.</value>
        public string AuthorityId
        {
            get
            {
                return this.txtAuthorityID.Text;
            }
            set
            {
                this.txtAuthorityID.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the container id.
        /// </summary>
        /// <value>The container id.</value>
        public string ContainerId
        {
            get
            {
                return this.txtContainerID.Text;
            }
            set
            {
                this.txtContainerID.Text = value;
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Handles the Validating event of the txtAuthorityID control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void txtAuthorityID_Validating(object sender, CancelEventArgs e)
        {
            if (this.OnValidation != null)
            {
                ValidationResult result = this.IsValidAuthorityID();
                ConnectionValidateEventArgs connectionValidateEventArgs = new ConnectionValidateEventArgs(result.GetMessage(), result.IsValid, "TextBox");            
                this.OnValidation(sender, connectionValidateEventArgs);
            }
        }

        /// <summary>
        /// Handles the Validating event of the txtContainerID control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void txtContainerID_Validating(object sender, CancelEventArgs e)
        {
            if (this.OnValidation != null)
            {
                ValidationResult result = this.IsValidContainerID();            
                ConnectionValidateEventArgs connectionValidateEventArgs = new ConnectionValidateEventArgs(result.GetMessage(), result.IsValid, "TextBox");            
                this.OnValidation(sender, connectionValidateEventArgs);
            }
        }

        #endregion

        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <returns></returns>
        public ValidationResult IsValid()
        {
            ValidationResult result = this.IsValidAuthorityID();
            if (!result.IsValid)
            {
                this.txtAuthorityID_Validating(this.txtAuthorityID, null);
            }

            ValidationResult ContainerIDValidation = this.IsValidContainerID();
            if (!ContainerIDValidation.IsValid)
            {
                this.txtContainerID_Validating(txtContainerID, null);
            }

            result.Merge(ContainerIDValidation);
            return result;
        }

        /// <summary>
        /// Determines whether [is valid authority ID].
        /// </summary>
        /// <returns></returns>
        private ValidationResult IsValidAuthorityID()
        {
            ValidationResult result = new ValidationResult(!this.txtAuthorityID.Text.Trim().IsNullOrEmpty());
            if (!result.IsValid)
            {
                result.Messages.Add("Authority ID is required");
            }

            return result;
        }

        /// <summary>
        /// Determines whether [is valid container ID].
        /// </summary>
        /// <returns></returns>
        private ValidationResult IsValidContainerID()
        {
            ValidationResult result = new ValidationResult(!this.txtContainerID.Text.Trim().IsNullOrEmpty());
            if (!result.IsValid)
            {
                result.Messages.Add("Container ID is required");
            }

            return result;
        }        
    }
}
